// pages/02admin/00waiter/waiter.js
var app = getApp()
var appData = app.globalData
var that
var co = require('../../js/common.js')
Page({
   data: {
      isShowInputLabelArr: [0, 0, 0, 0],
      waiter: '',
      waiterList: null,
      i: '',
      printerList: [],
      mode: '',
      buttonText: "添加服务员",
      pick_i: 0,
      edit_id: -1
   },

   onLoad: function (options) {
      that = this
      this.handleWaiterData()
      //监听服务员设置的通知
      // app.setNoticeWaiterList(function (data) {
      //    var i = that.data.i
      //    var list = that.data.waiterList
      //    if (i != '') {
      //       list[i] = data
      //    } else {
      //       list.push(data)
      //    }
      //    console.log('list', list)
      //    console.log('that.data.waiterList', that.data.waiterList)
      //    that.setData({
      //       waiterList: list
      //    })
      // })
   },

   addButtonChange: function () {
      this.setData({
         pick_i: 0,
         mode: this.data.mode == '' ? 'add' : '',
         buttonText: this.data.mode == '' ? '取消添加' : '添加服务员',
         edit_id: -1,
         isShowInputLabelArr: [0, 0, 0, 0]
      })
   },
   //添加或修改
   submitWaiterInfo: function (e) {
      var td = this.data
      var apiUrl                          //添加和修改服务员路径
      var param = e.detail.value          //获取form表单内容
      param.Biz_ID = appData.shopInfo.Biz_ID

      if (co.isEmpty(param.sort)) {
         param.sort = 0
      }
      if (td.edit_id == -1) {
         apiUrl = 'table/Tmg/insertWaiterInformation'
      } else {
         apiUrl = 'table/Tmg/updateWaiterInformation'
      }
      app.getJsonData(apiUrl, param, function (res) {
         console.log('【返回数据】===', res.msg)
         if (td.edit_id == -1) {
            that.addButtonChange()
         } else {
            that.setData({ edit_id: -1 })
         }
         that.setData({ isShowInputLabelArr: [0, 0, 0, 0] })
         that.onLoad()
      })
   },
   // 编辑服务员
   editwaiter: function (e) {
      var td = this.data
      var index = e.currentTarget.id
      var printer = td.printerList[index]
      this.setData({
         edit_id: td.edit_id == index ? -1 : index,
         isShowInputLabelArr: index > -1 ? [1, 1, 1, 1] : [0, 0, 0, 0],
         mode: '',
         buttonText: '添加服务员',
      })
      // console.log('this.data==', td)
   },
   // 删除服务员
   delConfirm: function (e) {
      app.confirm('重要提示', '确认删除吗？','取消', '删除', function (res) {
         // console.log('delConfirm: function', res)
         if (res) { that.delWaiter(e) }
      })
   },
   delWaiter: function (e) {
      if (app.globalData.loginMode == 'boss') {
         let i = e.currentTarget.id
         var list = this.data.waiterList
         var param = {
            user: list[i].user,
         }
         app.getJsonData('table/Tmg/deleteWaiterInformation', param, function (res) {
            // console.log("【获取返回信息】===",res.msg)
            list.splice(i, 1)
            that.setData({ waiterList: list })
         })
      } else {
         app.toast('需要掌柜登录才能操作')
      }
   },
   //获取服务员列表 
   handleWaiterData: function () {
      var param = { 
         Biz_ID: appData.shopInfo.Biz_ID
      }
      app.getJsonData('table/Tmg/getWaiterList', param, function (res) {
         if (res.code == 0) {
            that.setData({
               waiterList: res.data
            })
         }
      })
   },
})